package com.c.ctgproject.fragment.review.car

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.review.ReviewCarCommonAdapter
import com.c.ctgproject.base.BaseFragment
import com.c.ctgproject.entity.review.CarReviewEntity
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.initData
import com.c.ctgproject.utils.showToast
import kotlinx.android.synthetic.main.fragment_refuse_car.*

/**
 *作者:LiBW
 *创建日期: 2020/9/9
 *描述:报废车审核  已拒绝
 */
class RefuseCarFragment : BaseFragment() {

    private val mViewModel: RefuseCarViewModel by lazy { ViewModelProvider(requireActivity())[RefuseCarViewModel::class.java] }
    private var page = 1
    private var size = 10
    private var mList: ArrayList<CarReviewEntity.DataBean.ListBean> = ArrayList()
    private val mAdapter: ReviewCarCommonAdapter by lazy { ReviewCarCommonAdapter(requireContext(), mList) }

    override fun event() {
        mRecyclerView.adapter = mAdapter
        mRecyclerView.layoutManager = LinearLayoutManager(requireContext())

        mViewModel.result.observe(this, Observer {
            MyLog.d("回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                mRefresh.finishRefresh()
                mRefresh.finishLoadMore()
                val entity = getGson().fromJson(it, CarReviewEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                requireContext().showToast(data.msg)
            }
        })
        mViewModel.error.observe(this, Observer {
            mRefresh.finishRefresh()
            mRefresh.finishLoadMore()
            requireContext().showToast(it)
        })
    }

    override fun click() {
        //下拉刷新监听
        mRefresh.setOnRefreshListener {
            initData()
        }
        //上拉加载
        mRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getList(page, size)
        }
    }
    private fun initData() {
        page = 1
        mList.clear()
        mViewModel.getList(page, size)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }
    override fun getLayout() = R.layout.fragment_refuse_car

}